﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace QuanLyKho.BanHang
{
    public partial class ChiTietSanPham : Form
    {
        public static string MaID = "0";

        public ChiTietSanPham()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            var data = DataAll.SelectAll("SanPhamTrongCuaHang LEFT JOIN [dbo].[SanPhamHoanThien] ON [SanPhamTrongCuaHang].IDSpHoanThien = [SanPhamHoanThien].ID"
                , "SanPhamTrongCuaHang.SoLuong AS 'SoLuong1',*", " WHERE SanPhamHoanThien.ID=" + MaID);
            dtNgayThang.Value = Convert.ToDateTime(data.Rows[0]["NgayThang"]);
            txtLenhSanSuat.Text = data.Rows[0]["LenhSanXuat"].ToString();
            dtNgayLL.Value = Convert.ToDateTime(data.Rows[0]["NgayLL"]);
            txtNguoiTK.Text = data.Rows[0]["NguoiDangKy"].ToString();
            txtNguoiCat.Text = data.Rows[0]["NguoiCat"].ToString();
            //txtMaHang.Text = data.Rows[0]["MaHang"].ToString();
            txtSize.Text = data.Rows[0]["Size"].ToString();
            txtTenHang.Text = data.Rows[0]["TenHang"].ToString();
            spLan.EditValue = data.Rows[0]["LanSanXuat"].ToString();
            txtSoLuong.Text = data.Rows[0]["SoLuong1"].ToString();
            txtDonGia.Text = data.Rows[0]["DonGia"].ToString();
            txtMaHang.Text = data.Rows[0]["MaHang"].ToString();
            txtNhomHang.Text = data.Rows[0]["NhomHang"].ToString();
            txtLoaiHang.Text= data.Rows[0]["LoaiHang"].ToString();
            txtDonVi.Text = data.Rows[0]["DonVi"].ToString();
            txtMauSac.Text = data.Rows[0]["MauSac"].ToString();
            txtKhoVai.Text = data.Rows[0]["KhoVai"].ToString();
            dtTuNgay.Value = Convert.ToDateTime(data.Rows[0]["TuNgay"]);
            dtDenNgay.Value = Convert.ToDateTime(data.Rows[0]["DenNgay"]);
            dtNgayGCNhan.Value = Convert.ToDateTime(data.Rows[0]["NgayGCNhan"]);
            txtGiacSoDo.Text = data.Rows[0]["GiacSoDo"].ToString();
            txtGhiChu.Text = data.Rows[0]["GhiChu"].ToString();
            txtSoChungTu.Text = data.Rows[0]["SoChungTu"].ToString();
            LoadImage();
        }

        public void LoadImage()
        {
            var data = DataAll.SelectAll("DkChung_HinhAnh", "*", " WHERE MaAnh=N'" + txtMaHang.Text + "'");
            if (data.Rows.Count > 0)
            {
                var content = (byte[])data.Rows[0]["Anh"];
                if (content != null)
                {
                    Image newImage;
                    using (var ms = new MemoryStream(content, 0, content.Length))
                    {
                        ms.Write(content, 0, content.Length);
                        newImage = Image.FromStream(ms, true);
                    }
                    pbAnhKho.Image = newImage;
                }
                else
                {
                    pbAnhKho.Image = null;
                }
            }
            else
            {
                MessageBox.Show("Không tìm thấy mã ảnh", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
